//
//  SuDetailViewController.m
//  Delicious
//
//  Created by gy on 16/5/7.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "SuDetailViewController.h"
#import "SuDetailThreeTableViewCell.h"
#import "SuDetailFourTableViewCell.h"
#import "SuDetailCommentTableViewCell.h"
#import "SuComment.h"
#import "SuFirst.h"
#import "SuMaterial.h"
#import "SuMakingSteps.h"
#import "SuDetailFirstTableViewCell.h"
@interface SuDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *commentsArr;//评论的model
@property (nonatomic, strong) NSMutableArray *materialArr;//材料的model
@property (nonatomic, strong) NSMutableArray *makingStepsArr;//步骤的model
@property (nonatomic, strong) NSMutableArray *FirstArr; // 第一个cell的model
@property (nonatomic, assign) CGFloat height;
@end

@implementation SuDetailViewController

- (NSMutableArray *)commentsArr{
    if (!_commentsArr) {
        _commentsArr = [NSMutableArray array];
    }
    return _commentsArr;
}
- (NSMutableArray *)materialArr{
    if (!_materialArr) {
        _materialArr = [NSMutableArray array];
    }
    return _materialArr;
}
- (NSMutableArray *)makingStepsArr{
    if (!_makingStepsArr) {
        _makingStepsArr = [NSMutableArray array];
    }
    return _makingStepsArr;
}
- (NSMutableArray *)FirstArr{
    if (!_FirstArr) {
        _FirstArr = [NSMutableArray array];
    }
    return _FirstArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = self.model.name;
    // Do any additional setup after loading the view.
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = self.view.frame;
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.imageUrl]];
    [self.view addSubview:imageView];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 4 * 3)];
    view.backgroundColor = [UIColor clearColor];
//    view.alpha = 0;
    self.tableView.tableHeaderView = view;

    [self.tableView registerNib:[UINib nibWithNibName:@"SuDetailFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"SuDetailFirst"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SuDetailThreeTableViewCell" bundle:nil] forCellReuseIdentifier:@"SuDetailThree"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SuDetailFourTableViewCell" bundle:nil] forCellReuseIdentifier:@"SuDetailFour"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SuDetailCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"SuDetailComment"];

    
    [self.view addSubview:self.tableView];
    [self getData];
    }
- (void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *urlStr = [kSuDishDetail stringByAppendingString:[NSString stringWithFormat:@"%@",self.model.ID]];
//    NSLog(@"%@", urlStr);
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        // 评论的model
        NSArray *arr1 = responseObject[@"data"][@"comments"];
        NSLog(@"%@", arr1);
        for (NSDictionary *d in arr1) {
            SuComment *model = [[SuComment alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.commentsArr addObject:model];
        }
        //材料的model
        NSArray *arr2 = responseObject[@"data"][@"ingredients"];
        for (NSDictionary *dic in arr2)
        {
             SuMaterial *meterial = [[SuMaterial alloc]init];
            [meterial setValuesForKeysWithDictionary:dic];
            [self.materialArr addObject:meterial];
        }


        //步骤
        NSArray *arr3 = responseObject[@"data"][@"makingSteps"];
        for (NSDictionary *d in arr3) {
            SuMakingSteps *model1 = [[SuMakingSteps alloc]init];
            [model1 setValuesForKeysWithDictionary:d];
            [self.makingStepsArr addObject:model1];
        }
        // first
        NSDictionary *d = responseObject[@"data"];
        SuFirst *model2 = [[SuFirst alloc]init];
        [model2 setValuesForKeysWithDictionary:d];
        [self.FirstArr addObject:model2];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return self.materialArr.count;
    }else if (section == 2) {
        return self.makingStepsArr.count;
    } else {
        return self.commentsArr.count;
    }

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *str = nil;
    if (section == 1) {
       str = @"食材";

    }
    if (section == 3) {
        str = @"评论";
    }
    return str;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SuDetailFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuDetailFirst" forIndexPath:indexPath];
        SuFirst *model = self.FirstArr.lastObject;
        self.height = [cell heightForText:model.Description];
        cell.model = model;
        return cell;
    }else if (indexPath.section == 1) {
        SuDetailThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuDetailThree" forIndexPath:indexPath];
        SuMaterial *model = self.materialArr[indexPath.row];
        cell.model = model;
        return cell;
    } else if (indexPath.section == 2) {
     SuDetailFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuDetailFour" forIndexPath:indexPath];
        SuMakingSteps *model = self.makingStepsArr[indexPath.row];
        cell.model = model;
        return cell;
    } else {
        SuDetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuDetailComment" forIndexPath:indexPath];
        SuComment *model = self.commentsArr[indexPath.row];
        cell.model = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1)
    {

        return 30;
    }
    else if (indexPath.section == 2)
    {
        return 152;
    }
    else if (indexPath.section == 3)
    {
        return 115;
    }else
    {
        return self.height + 150;

    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
